﻿Imports Microsoft.VisualBasic
Imports System.Web.UI
Imports System.Web.UI.WebControls


''' <summary>
''' WARNING! Post back events do not work properly.
''' ToolKitImage is a Javascript enabled Custom Control
''' Automatic Javascipt Rollovers
''' </summary>
''' <remarks></remarks>
Public Class ToolKitImage
    Inherits Image
    Implements IPostBackEventHandler

    ''' <summary>
    ''' Define the Click event.
    ''' </summary>
    ''' <remarks></remarks>
    Public Event Click As EventHandler

    ''' <summary>
    ''' Invoke delegates registered with the Click event.
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overridable Sub OnClick(ByVal e As EventArgs)
        RaiseEvent Click(Me, e)
    End Sub

    ''' <summary>
    ''' Url of the Rollover image
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ImageUrlOver() As String
        Get
            Return _ImageUrlOver
        End Get
        Set(ByVal value As String)
            _ImageUrlOver = value
        End Set
    End Property
    Private _ImageUrlOver As String = ""

    ''' <summary>
    ''' Javascript OnMouseOver Event
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property OnClientMouseOver() As String
        Get
            Return _OnClientMouseOver
        End Get
        Set(ByVal value As String)
            _OnClientMouseOver = value
        End Set
    End Property
    Private _OnClientMouseOver As String = ""

    ''' <summary>
    ''' Javascript OnMouseOut Event
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property OnClientMouseOut() As String
        Get
            Return _OnClientMouseOut
        End Get
        Set(ByVal value As String)
            _OnClientMouseOut = value
        End Set
    End Property
    Private _OnClientMouseOut As String = ""

    ''' <summary>
    ''' Javascript OnClick Event
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property OnClientClick() As String
        Get
            Return _OnClientClick
        End Get
        Set(ByVal value As String)
            _OnClientClick = value
        End Set
    End Property
    Private _OnClientClick As String = ""

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property HRef() As String
        Get
            Return _HRef
        End Get
        Set(ByVal value As String)
            _HRef = value
        End Set
    End Property
    Private _HRef As String = ""

    ''' <summary>
    ''' Loads Javascript into Image HTML markup
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        MyBase.OnPreRender(e)
        OnClientClick += Me.Page.ClientScript.GetPostBackEventReference(Me, "Click")

        If ImageUrlOver <> "" Then OnClientMouseOver = "this.src='" + ImageUrlOver + "';" + OnClientMouseOver
        If ImageUrlOver <> "" Then OnClientMouseOut = "this.src='" + ImageUrl + "';" + OnClientMouseOut
        If OnClientMouseOver <> "" Then Me.Attributes.Add("onmouseover", OnClientMouseOver)
        If OnClientMouseOut <> "" Then Me.Attributes.Add("onmouseout", OnClientMouseOut)
        If OnClientClick <> "" Then Me.Attributes.Add("onclick", OnClientClick)
    End Sub

    ''' <summary>
    '''  Define the method of IPostBackEventHandler that raises change events.
    ''' </summary>
    ''' <param name="eventArgument"></param>
    ''' <remarks></remarks>
    Public Sub RaisePostBackEvent(ByVal eventArgument As String) _
       Implements IPostBackEventHandler.RaisePostBackEvent
        If HRef <> "" AndAlso eventArgument Is "Click" Then
            HttpContext.Current.Response.Redirect(HRef)
            Me.OnClick(EventArgs.Empty)
        End If
    End Sub
End Class

